//
//  HWMineVipView.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/22.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWMineVipView: UIView {
    @objc var bgImageView:UIImageView!
    @objc var surplusLabel:UILabel!
    @objc var progressBarBg:UIView!
    @objc var progressBar:YLProgressBar!
    @objc var leftLabel:UILabel!
    @objc var rightLabel:UILabel!
    @objc var lookButton:UIButton!
    var lookBgView:UIView!
    var lookLabel:UILabel!
    var lookImageView:UIImageView!

    var leftCircleView:UIView!
    var rightCircleView:UIView!
    
    var model:HWMineModel?{
        didSet {
//            bgImageView.sd_setImage(with: URL(string: model?.config.huiyuan ?? ""), completed: nil)
//            
//            surplusLabel.text = "当前VIP值：\(model?.config.exp ?? 0)，距离下一级剩余:\((model?.config.last_exp ?? 0)-(model?.config.exp ?? 0))"
//            progressBar.progress = CGFloat(model?.config.exp ?? 0) / CGFloat(model?.config.last_exp ?? 0)
//            leftLabel.text = "当前\(model?.config.vip ?? "")"
//            rightLabel.text = model?.config.last_vip
            
        }
    }
    
    var lookButtonClick:(()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        UIColor.gradientColor(view: progressBarBg, colors: [UIColor.colorWithHexString(hexString: "#F4D58D").cgColor,
                                                            UIColor.colorWithAlphaHexString(hexString: "#D2A647", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
        
        UIColor.gradientColor(view: lookBgView, colors: [UIColor.colorWithHexString(hexString: "#F3D185").cgColor,
                                                            UIColor.colorWithAlphaHexString(hexString: "#FBE6AB", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
    }
}

extension HWMineVipView {
    private func initUI() -> Void {
        bgImageView = UIImageView()
        bgImageView.backgroundColor = .clear
        addSubview(bgImageView)
        
        surplusLabel = HWLabel.createBoldLabel(title: "", color: "#C29B5B", font: 10*widthScale)
        addSubview(surplusLabel)
        
        progressBarBg = UIView()
        progressBarBg.layer.cornerRadius = 2*widthScale
        progressBarBg.layer.masksToBounds = true
        addSubview(progressBarBg)
        
        progressBar = YLProgressBar()
        progressBar.indicatorTextDisplayMode = .none
        progressBar.progressTintColors = [UIColor.colorWithHexString(hexString: "#F1CD7E"),UIColor.colorWithHexString(hexString: "#C18C19")]
        progressBar.trackTintColor = .clear
        progressBar.cornerRadius = 0
        progressBar.hideStripes = true
        progressBar.progress = 0
        progressBar.hideGloss = true
        progressBar.progressStretch = true
        progressBar.progressBarInset = 0
        addSubview(progressBar)
        
        leftLabel = HWLabel.createLabel(title: "", color: "#C29B5B", font: 8*widthScale)
        addSubview(leftLabel)
        
        rightLabel = HWLabel.createLabel(title: "", color: "#C29B5B", font: 8*widthScale)
        addSubview(rightLabel)
        
        lookBgView = UIView()
        lookBgView.layer.cornerRadius = 2*widthScale
        lookBgView.layer.masksToBounds = true
        addSubview(lookBgView)
        
        lookLabel = HWLabel.createLabel(title: "权益抢先看", color: "#666666", font: 10*widthScale)
        lookBgView.addSubview(lookLabel)
        
        lookImageView = UIImageView()
        lookImageView.image = UIImage(named: "mineVipViewlookButton")
        lookBgView.addSubview(lookImageView)
        
        lookButton = UIButton(type: .custom)
        lookButton.addTarget(self, action: #selector(lookButtonClick(_:)), for: .touchUpInside)
        lookBgView.addSubview(lookButton)
        
        leftCircleView = UIView()
        leftCircleView.backgroundColor = .colorWithHexString(hexString: "#FFF0BF")
        leftCircleView.layer.cornerRadius = 8/2*widthScale
        leftCircleView.layer.borderWidth = 2*widthScale
        leftCircleView.layer.borderColor = UIColor.colorWithHexString(hexString: "#F4D58D").cgColor
        addSubview(leftCircleView)
        
        rightCircleView = UIView()
        rightCircleView.backgroundColor = .colorWithHexString(hexString: "#F5D892")
        rightCircleView.layer.cornerRadius = 8/2*widthScale
        rightCircleView.layer.borderWidth = 2*widthScale
        rightCircleView.layer.borderColor = UIColor.colorWithHexString(hexString: "#D2A647").cgColor
        addSubview(rightCircleView)
        
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        surplusLabel.snp.makeConstraints { make in
            make.left.equalTo(21*widthScale)
            make.top.equalTo(15*widthScale)
        }
        
        leftCircleView.snp.makeConstraints { make in
            make.centerY.equalTo(progressBarBg)
            make.width.height.equalTo(8*widthScale)
            make.right.equalTo(progressBarBg.snp.left).offset(2)
        }
        
        rightCircleView.snp.makeConstraints { make in
            make.centerY.equalTo(progressBarBg)
            make.width.height.equalTo(8*widthScale)
            make.left.equalTo(progressBarBg.snp.right).offset(-2)
        }
        
        progressBarBg.snp.makeConstraints { make in
            make.left.equalTo(27*widthScale)
            make.top.equalTo(surplusLabel.snp.bottom).offset(5*widthScale)
            make.height.equalTo(4*widthScale)
            make.right.equalTo(lookButton.snp.left).offset(-44*widthScale)
        }
        
        progressBar.snp.makeConstraints { make in
            make.left.equalTo(27*widthScale)
            make.top.equalTo(surplusLabel.snp.bottom).offset(5*widthScale)
            make.height.equalTo(4*widthScale)
            make.right.equalTo(lookButton.snp.left).offset(-44*widthScale)
        }
        
        leftLabel.snp.makeConstraints { make in
            make.left.equalTo(leftCircleView)
            make.top.equalTo(progressBarBg.snp.bottom).offset(5*widthScale)
        }
        
        rightLabel.snp.makeConstraints { make in
            make.centerX.equalTo(rightCircleView)
            make.top.equalTo(progressBarBg.snp.bottom).offset(5*widthScale)
        }
        
        lookBgView.snp.makeConstraints { make in
            make.right.equalTo(-17*widthScale)
            make.width.equalTo(63*widthScale)
            make.height.equalTo(15*widthScale)
            make.centerY.equalTo(progressBarBg)
        }
        
        lookLabel.snp.makeConstraints { make in
            make.left.equalTo(3*widthScale)
            make.centerY.equalToSuperview()
        }
        
        lookImageView.snp.makeConstraints { make in
            make.left.equalTo(lookLabel.snp.right).offset(3*widthScale)
            make.centerY.equalTo(lookLabel)
            make.width.equalTo(6*widthScale)
            make.height.equalTo(8*widthScale)
        }
        
        lookButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension HWMineVipView {
    @objc func lookButtonClick(_ button:UIButton) -> Void {
        self.lookButtonClick?()
    }
}
